import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import { buildSchema } from 'graphql';

const schema = buildSchema(`
    type Query {
        hello: String
        age: Int
    }
`);

const root = {
    hello: () => {
        return 'Hello Express World!';
    },
    age: () => {
        return Math.floor(Math.random() * 10) + 5;
    },
};

const app = express();
app.use(
    '/graphql',
    graphqlHTTP({
        schema,
        rootValue: root,
        graphiql: true,
    })
);

app.listen(4000);
console.log('running a GraphQL API server at http://localhost:4000/graphql');
